package com.zlll.winner.service.sysUser;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zlll.winner.business.model.sysUser.SysPermissionRole;
import com.zlll.winner.business.service.sysUser.ISysRoleService;
import com.zlll.winner.enums.CommonEnum;
import com.zlll.winner.mapper.sysUser.SysPermissionRoleMapper;
import com.zlll.winner.business.service.sysUser.ISysPermissionRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单权限 服务实现类
 * </p>
 *
 * @author ricky
 * @since 2019-09-06
 */
@Service
public class SysPermissionRoleServiceImpl extends ServiceImpl<SysPermissionRoleMapper, SysPermissionRole> implements ISysPermissionRoleService {

    @Autowired
    private SysPermissionRoleMapper sysPermissionRoleMapper;
    @Autowired
    private ISysRoleService iSysRoleService;

    /**
     * 组装菜单权限
     * @param menuIds
     * @param userId
     * @param name
     * @param roleId
     * @return
     */
    public void addPermissionRole(List<Long> menuIds, Long userId, String name, Long roleId){
        List<SysPermissionRole> permissionRoles = new ArrayList<>();
        for (Long menuId : menuIds) {
            SysPermissionRole permissionRole = new SysPermissionRole();
            permissionRole.setRoleId(roleId);
            permissionRole.setMenuId(menuId);
            permissionRole.setCreateTime(System.currentTimeMillis());
            permissionRole.setCreateUserId(userId);
            permissionRole.setCreateUser(name);
            permissionRole.setIsDel(CommonEnum.DelEnum.UNDEL.val());
            permissionRoles.add(permissionRole);
        }
        sysPermissionRoleMapper.batchInsert(permissionRoles);
    }

    /**
     * 根据角色id获取角色的权限列表信息
     * @param roleId
     * @return
     */
    @Override
    public List<SysPermissionRole> getPermissionRoleList(Long roleId) {
        QueryWrapper<SysPermissionRole> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", roleId);
        wrapper.eq("is_del",CommonEnum.DelEnum.UNDEL.val());
        return sysPermissionRoleMapper.selectList(wrapper);
    }

    /**
     * 获取用户的权限
     * @param roleId
     * @return
     */
    @Override
    public List<SysPermissionRole> getPermissionRoleAllList(Long roleId) {
        QueryWrapper<SysPermissionRole> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", roleId);
        return sysPermissionRoleMapper.selectList(wrapper);
    }

    /**
     * 删除角色权限
     * @param id
     * @param userId
     * @param name
     */
    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
    public void deletePermissionRole(Long id, Long userId, String name) {
        // TODO: 2019-09-07 此处需要处理删除用户的角色
        iSysRoleService.deleteRole(id,userId,name);
        sysPermissionRoleMapper.deleteByRoleId(id);
    }
}
